/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.service;

import com.alxbrendmaquinarias.model.Product;
import com.alxbrendmaquinarias.model.ProductShop;
import com.alxbrendmaquinarias.model.Shop;
import com.alxbrendmaquinarias.model.Storehouse;
import com.alxbrendmaquinarias.model.StorehouseProduct;
import com.alxbrendmaquinarias.repository.GenericDAO;
import com.alxbrendmaquinarias.util.VarUtil;
import com.alxbrendmaquinarias.util.faces.FacesUtil;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Max
 */
@Service
public class StorehouseServiceImpl implements StorehouseService{
    @Autowired
    GenericDAO genericDAO;

    @Override
    public List list(String namedQuery) throws Exception {
        return genericDAO.findWithNamedQuery(namedQuery);
    }

    @Override
    public Storehouse save(Storehouse storehouse) throws Exception {
        
        //TRAER LOS PRECIOS DE OTRA TIENDA. SI ES QUE EXISTE
        Storehouse storehouseTemp = genericDAO.create(storehouse);
        List<Product> list = genericDAO.findWithNamedQuery("Product.findAll");
        for (int i = 0; i < list.size(); i++) {
            StorehouseProduct st = new StorehouseProduct();
            st.setAmount(0);
            st.setProductId(list.get(i));
            st.setStorehouseId(storehouseTemp);
            genericDAO.create(st);
        }
        FacesUtil.addMessage("Actualización", "Se ha añadido " + list.size() + " productos al almacén", FacesMessage.SEVERITY_INFO);
        return storehouseTemp;
    }

    @Override
    public void update(Storehouse storehouse) throws Exception {
        genericDAO.update(storehouse);
    }

    @Override
    public void remove(Storehouse storehouse) throws Exception {
        genericDAO.delete(storehouse);
    }
}
